﻿using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Xml.Linq;
using System;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Domian.Entities.Account;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class ShowHandler : IRequestHandler<ShowCommand, ApiPaging<ShowDTO>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<ShiModel> shiRepo;
        private readonly IBaseRepository<ShengModel> shengRepo;
        private readonly IBaseRepository<ShiModel> shigRepo;
        private readonly IBaseRepository<XianModel> xianRepo;

        public ShowHandler(IBaseRepository<AccountModel> AccountRepo, IBaseRepository<ShengModel> shengRepo, IBaseRepository<ShiModel> shiRepo, IBaseRepository<XianModel> xianRepo)
        {
            accountRepo = AccountRepo;
            this.shengRepo = shengRepo;
            this.shiRepo = shiRepo;
            this.xianRepo = xianRepo;
        }
        public Task<ApiPaging<ShowDTO>> Handle(ShowCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiPaging<ShowDTO>();
            var account = accountRepo.GetAll().Where(x => x.IsDel == false);
            var sheng = shengRepo.GetAll().Where(x => x.IsDel == false);
            var shi = shiRepo.GetAll().Where(x => x.IsDel == false);
            var xian = xianRepo.GetAll().Where(x => x.IsDel == false);
            var linq = from a in account
                       join b in sheng on a.Shengid equals b.Shengid
                       join c in shi on a.Shiid equals c.Shiid
                       join d in xian on a.xiangid equals d.xiangid
                       select new ShowDTO
                       {
                           Shengid = a.Shengid,
                           ShengName = b.ShengName,
                           Shiid = a.Shiid,
                           ShiName = c.ShiName,
                           xiangid = a.xiangid,
                           XianName = d.XianName,
                           Id = a.Id,
                           Name = a.Name,
                           Sex = a.Sex,
                           Addr = a.Addr,
                           CreateTime = a.CreateTime,
                           CreatePeople = a.CreatePeople,
                           UpdateTime = a.UpdateTime,
                           UpdatePeople = a.UpdatePeople,

                       };
            var totalcount = linq.Count();
            var totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.PageSize);
            linq = linq.OrderBy(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            res.totalcount = totalcount;
            res.totalpage = totalpage;
            res.Message = "显示成功";
            res.Code = ApiEnum.Ok;
            res.Data = linq.ToList();
            return Task.FromResult(res);
        }
    }
}
